/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package tienda.zapateria.siszap.gui.control;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

import static org.rx.cr.util.Utilitarios.addEnterDoclickEvent;
import static org.rx.cr.util.Utilitarios.alinearDatosColumnaTablaCentro;
import static org.rx.cr.util.Utilitarios.formateaAnchoColumnaTabla;
import tienda.zapateria.siszap.bl.BLArticulo;
import tienda.zapateria.siszap.bl.BLRelacionTalla;
import tienda.zapateria.siszap.gui.JF_Principal;
import tienda.zapateria.siszap.gui.control.model.ModelRelacionTalla;

/**
 *
 * @author Mysham
 */
public class JIFAsignarArticuloCompra extends javax.swing.JInternalFrame {

    JF_Principal parent;
    ModelRelacionTalla modelRelacionTalla;
    BLArticulo blArticulo;
    BLRelacionTalla bLRelacionTalla;

    /**
     * Creates new form JIFAdministrarInfraestructura
     */
    public JIFAsignarArticuloCompra(JF_Principal parent) {
        initComponents();
        this.parent = parent;
        modelRelacionTalla = new ModelRelacionTalla();
        jtArticulo.setModel(modelRelacionTalla);
        bLRelacionTalla = new BLRelacionTalla(parent.conf);
        personalizaTabla();
        blArticulo = new BLArticulo(parent.getConf());
        try {
            listarTabla();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "Error al listar la lista de artículos." + e, "Atención.", JOptionPane.ERROR_MESSAGE);

        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtArticulo = new javax.swing.JTable();
        txfBuscar = new javax.swing.JTextField();
        jbBuscar = new javax.swing.JButton();
        jbAgregar = new javax.swing.JButton();
        jbCargar = new javax.swing.JButton();

        setClosable(true);
        setIconifiable(true);
        setTitle("ARTÍCULO");
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                formComponentShown(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "ASIGNACIÓN DE ARTÍCULO", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, new java.awt.Color(0, 0, 0)));

        jtArticulo.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jtArticulo.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jtArticuloMouseReleased(evt);
            }
        });
        jScrollPane1.setViewportView(jtArticulo);

        jbBuscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tienda/zapateria/siszap/imagen/Buscar.png"))); // NOI18N
        jbBuscar.setBorder(null);
        jbBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbBuscarActionPerformed(evt);
            }
        });

        jbAgregar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tienda/zapateria/siszap/imagen/small_agregar.png"))); // NOI18N
        jbAgregar.setBorder(null);
        jbAgregar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbAgregarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(txfBuscar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbAgregar, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 770, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(txfBuscar)
                        .addComponent(jbBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jbAgregar, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 256, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );

        jbCargar.setText("CARGAR");
        jbCargar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbCargarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jbCargar)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 301, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbCargar)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jtArticuloMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jtArticuloMouseReleased
        // TODO add your handling code here:
        jbCargar.setEnabled(true);
    }//GEN-LAST:event_jtArticuloMouseReleased

    private void jbBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbBuscarActionPerformed
        try {
            listarTabla();
        } catch (Exception ex) {
            Logger.getLogger(JIFAsignarArticuloCompra.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jbBuscarActionPerformed

    private void jbCargarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbCargarActionPerformed
        // TODO add your handling code here:

        if (jtArticulo.getSelectedRow() >= 0) {
            parent.jIF_AdministrarCompra.agregarArticulo(modelRelacionTalla.get(jtArticulo.getSelectedRow()));
        }

    }//GEN-LAST:event_jbCargarActionPerformed

    private void formComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentShown
        // TODO add your handling code here:
        try {
            // TODO add your handling code here:
            listarTabla();
        } catch (Exception ex) {
            Logger.getLogger(JIFAsignarArticuloCompra.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_formComponentShown

    private void jbAgregarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbAgregarActionPerformed
        // TODO add your handling code here:
        parent.insertarInternalFrame(parent.jIF_AdministrarArticulo);
    }//GEN-LAST:event_jbAgregarActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbAgregar;
    private javax.swing.JButton jbBuscar;
    private javax.swing.JButton jbCargar;
    private javax.swing.JTable jtArticulo;
    private javax.swing.JTextField txfBuscar;
    // End of variables declaration//GEN-END:variables
  public void _agregaValidadoresControl() {
        addEnterDoclickEvent(new Object[][]{{txfBuscar, jbBuscar}});

    }

    private void personalizaTabla() {
        alinearDatosColumnaTablaCentro(0, jtArticulo);
        alinearDatosColumnaTablaCentro(1, jtArticulo);
        alinearDatosColumnaTablaCentro(2, jtArticulo);
        alinearDatosColumnaTablaCentro(3, jtArticulo);
        alinearDatosColumnaTablaCentro(4, jtArticulo);
        alinearDatosColumnaTablaCentro(5, jtArticulo);
        alinearDatosColumnaTablaCentro(6, jtArticulo);
        alinearDatosColumnaTablaCentro(7, jtArticulo);
        alinearDatosColumnaTablaCentro(8, jtArticulo);

        formateaAnchoColumnaTabla(0, jtArticulo, 50);
        formateaAnchoColumnaTabla(1, jtArticulo, 80);
        formateaAnchoColumnaTabla(2, jtArticulo, 80);
        formateaAnchoColumnaTabla(3, jtArticulo, 80);
        formateaAnchoColumnaTabla(4, jtArticulo, 80);
        formateaAnchoColumnaTabla(5, jtArticulo, 50);
        formateaAnchoColumnaTabla(6, jtArticulo, 50);
        formateaAnchoColumnaTabla(7, jtArticulo, 50);
        formateaAnchoColumnaTabla(8, jtArticulo, 50);

    }

    public void listarTabla() throws Exception {
        try {
            modelRelacionTalla.addAll(bLRelacionTalla.buscarReferencia(txfBuscar.getText()));
            personalizaTabla();
            jbCargar.setEnabled(false);
        } catch (Exception e) {
            Logger.getLogger(JIFAsignarArticuloCompra.class.getName()).log(Level.SEVERE, null, e);
        }

    }
}
